<?php echo "<?php\n"; ?>

<?php if(!empty($relations)): ?>

<?php foreach($relations as $name=>$relation): ?>
    <?php
	if (preg_match("~^array\(self::([^,]+), '([^']+)', '([^']+)'\)$~", $relation, $matches))
    {
        $relationType = $matches[1];
        $relationModel = $matches[2];

        switch($relationType){
            case 'HAS_ONE':
                echo $relationModel.' $'.$name."\n";
            break;
            case 'BELONGS_TO':
                echo $relationModel.' $'.$name."\n";
            break;
            case 'HAS_MANY':
                echo $relationModel.'[] $'.$name."\n";
            break;
            case 'MANY_MANY':
                echo $relationModel.'[] $'.$name."\n";
            break;
            default:
                echo 'mixed $'.$name."\n";
        }
	}
    ?>
<?php endforeach; ?>
<?php endif; ?>

class <?php echo $modelClass; ?> extends <?php echo $this->baseClass."\n"; ?>
{
	public static function model($className=__CLASS__)
	{
            return parent::model($className);
	}
<?php if($connectionId!='db'):?>

	public function getDbConnection()
	{
            return Yii::app()-><?php echo $connectionId ?>;
	}
<?php endif?>

	public function tableName()
	{
            return '<?php echo $tableName; ?>';
	}

	public function rules()
	{

            return array(
<?php foreach($rules as $rule): ?>
			<?php echo $rule.",\n"; ?>
<?php endforeach; ?>
			array('<?php echo implode(', ', array_keys($columns)); ?>', 'safe', 'on'=>'search'),
		);
	}
        
	public function relations()
	{
            return array(
<?php foreach($relations as $name=>$relation): ?>
			<?php echo "'$name' => $relation,\n"; ?>
<?php endforeach; ?>
		);
	}

	public function attributeLabels()
	{
            return array(
<?php foreach($labels as $name=>$label): ?>
                    <?php echo "'$name' => Yii::t('system','$label'),\n"; ?>
<?php endforeach; ?>
            );
	}
        
        public function dropDownItemsAll(){
            $items= Array();
            $model = self::model()->findAll();
            foreach ($model as $value) {
                $items[$value->id] = $value->id;
            }
            return $items;
        }
        
	public function search()
	{	
            $criteria=new CDbCriteria;

<?php
foreach($columns as $name=>$column)
{
    if($column->type==='string')
    {
            echo "\t\t\$criteria->compare('$name',\$this->$name,true);\n";
    }
    else
    {
            echo "\t\t\$criteria->compare('$name',\$this->$name);\n";
    }
}
?>

            return new CActiveDataProvider($this, array(
                    'criteria'=>$criteria,
            ));
	}
}


//    public static function allLang(){
//        $dataReader=Yii::app()->db->createCommand()
//        ->select('l,name')
//        ->from(array('Language'))
//        ->queryAll();
//        return $dataReader;
//    }
//    
//    public static function countLang(){
//        $sql = "SELECT COUNT(*) as count FROM `Language`";
//        $dataReader=Yii::app()->db->createCommand($sql)->queryRow();
//        return $dataReader['count'];
//    }
//    
//    public static function allExcept($except_id){
//        $dataReader=Yii::app()->db->createCommand()
//        ->select('*')
//        ->from(array('Language'))
//        ->where(array('and', '`primary` <> :primary', array('not in','id',$except_id)),array(':primary'=>'1'))
//        ->queryAll();
//        return $dataReader;
//    }